package com.teacher.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.teacher.common.CommonResult;
import com.teacher.common.JwtTokenUtil;
import com.teacher.pojo.dto.devUser.DevLoginDto;
import com.teacher.pojo.dto.deviceUser.DevRegisterDto;
import com.teacher.pojo.entity.DeviceUser;
import com.teacher.mapper.DeviceUserMapper;
import com.teacher.pojo.vo.devUser.DevLoginVo;
import com.teacher.security.entity.security.RedisUserInfo;
import com.teacher.security.service.IRedisService;
import com.teacher.service.IDeviceUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author jiangzhuohang
 * @since 2025-04-11
 */
@Service
public class DeviceUserServiceImpl extends ServiceImpl<DeviceUserMapper, DeviceUser> implements IDeviceUserService {

    @Value("${jwt.expiration}")
    private Long expiration;

    @Autowired
    private JwtTokenUtil jwtTokenUtil;

    @Autowired
    private DeviceUserMapper deviceUserMapper;

    @Autowired
    private PasswordEncoder passwordEncoder;

    @Autowired
    private IRedisService iRedisService;

    @Override
    public CommonResult<String> register(DevRegisterDto devRegisterDto) {
        DeviceUser deviceUser = deviceUserMapper.selectOne(new QueryWrapper<DeviceUser>().lambda().eq(DeviceUser::getUserName, devRegisterDto.getUserName()));
        if (!ObjectUtil.isEmpty(deviceUser)){
            return CommonResult.failed("用户已存在");
        }
        DeviceUser newUser = new DeviceUser();
        BeanUtil.copyProperties(devRegisterDto,newUser);
        newUser.setPassword(passwordEncoder.encode(devRegisterDto.getPassword()));

        int insert = deviceUserMapper.insert(newUser);

        if (insert == 1) {
            return CommonResult.success("注册成功");
        }
        return CommonResult.success("注册失败");

    }

    @Override
    public CommonResult<DevLoginVo> DevLogin(DevLoginDto devLoginDto) {
        DeviceUser deviceUser = deviceUserMapper.selectOne(new QueryWrapper<DeviceUser>().lambda().eq(DeviceUser::getUserName, devLoginDto.getUserName()));
        if (ObjectUtil.isEmpty(deviceUser)){
            return CommonResult.failed("用户不存在");
        }
        if (passwordEncoder.matches(deviceUser.getPassword(),devLoginDto.getPassword())){
            return CommonResult.failed("密码错误");
        }
        DevLoginVo loginUser = new DevLoginVo();
        BeanUtil.copyProperties(deviceUser,loginUser);
        String token = jwtTokenUtil.generateToken(deviceUser.getUserName());
        loginUser.setToken(token);

        RedisUserInfo redisUserInfo = new RedisUserInfo();
        redisUserInfo.setToken(token);
        iRedisService.set(deviceUser.getUserName(), JSONUtil.toJsonStr(redisUserInfo));
        iRedisService.expire(deviceUser.getUserName(), expiration);
        return CommonResult.success(loginUser);
    }
}
